<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Cache;
use think\Route;
use common\SmsDemo;


class Index extends Ubase
{

    public static function are_null () 
    {
        $args = func_get_args ();
        foreach ( $args as $arg )
            if ( empty ($arg) )
                return false;
        return true;
    }
    
    public function index ()
    {
        $user = [];
        $g_title = db('notice')->order('create_time desc')->where('n_keywords',0)->field('n_title')->limit(3)->select();
        $d_title = db('notice')->order('create_time desc')->where('n_keywords',1)->field('n_title')->limit(3)->select();
        $n_title = db('notice')->order('create_time desc')->where('n_keywords',2)->field('n_title')->limit(3)->select();
        $this->assign('gnotice',$g_title);
        $this->assign('dnotice',$d_title);
        $this->assign('nnotice',$n_title);
        if (Session::has('aid')) {
            $user = db('ns_user')->where('id',Session::get('aid'))->find();
        }
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function login ()
    {
        if (is_post()) {
            $post = input('post.');
            if (input('post.mobile')&&input('post.password')) {
                $info = db('ns_user')->where('mobile',$post['mobile'])->find();
                if ($info) {
                    if (encrypt($post['password']) == $info['password']) {
                        if ($info['del_status']) {
                            if ($info['status']) {
                                session('aid', $info['id']);
                                session('ad_name',$info['username']);
                                $log = array();
                                $log['uid'] = $info['id'];
                                $log['umobile'] = $info['mobile'];
                                $log['uname'] = $info['username'];
                                $log['note'] = '登录系统';
                                $log['logtime'] = date('Y-m-d H:i:s',time());
                                Cache::set('log'.$info['id'],'1',300);
                                db('userlog')->insert($log);
                                $this->success('登录成功');
                            }
                            $this->error('账号冻结中');
                        }
                        $this->error('此账号已禁用');
                    }
                    $this->error('账号或密码错误');
                }
                $this->error('您还未注册哦');
            }
            $this->error('不可以为空哦');
        }
        $this->error('您属于非法请求哦');
    }

    public function signout ()
    {
        if (is_post()) {
            if(Session::has('aid')) {
                $id = session('aid');
                Session::delete('aid');
                Cache::rm('log'.$id);
            }
            $this->success('成功退出');
        }
        $this->error('您属于非法请求哦');
    }

    public function signup()
    {
        if (is_post()) {
            $post = input('post.');
            if (self::are_null($post)) {
                if($post['code'] == cookie('code')){
                    unset($post['code']);
                    $field = '/^[A-Z]\S{7,17}$/';
                    if (preg_match($field,$post['password'])) {
                        if ($post['password'] == $post['rpwd']) {
                            if(cookie('mobile') == $post['mobile']) {
                                $info = db('ns_user')->where('mobile',$post['pmobile'])->find();
                                if ($info) { 
                                    unset($post['pmobile']);
                                    unset($post['rpwd']);
                                    $post['password'] = encrypt($post['password']);
                                    $post['user_group'] = 1;
                                    $post['sign_time'] = date('Y-m-d H:i:s',time());
                                    $post['pid'] = $info['id'];
                                    $count = db('ns_user')->where('mobile',$post['mobile'])->count();
                                    if($count < 1) {
                                        if (db('ns_user')->insert($post)) {
                                            $this->success('注册成功，请登录');
                                        }
                                    }
                                    $this->error('请稍后重试');
                                }
                                $this->error('推荐人不存在');
                            }
                            cookie('mobile',null);
                            $this->error('注册手机号已更改,请重新验证');
                        }
                        $this->error('两次密码不一致');
                    }
                    $this->error('密码格式不正确');
                }
                $this->error('短信验证错误');
            }
            $this->error('不可以为空哦');
        }
        $this->error('您属于非法请求哦');
    }

    public function code()
    {
        if (is_post()) {
            $post = input('post.');
            if (self::are_null($post)) {
                $validate = validate('Vnsuser');
                $rst = $validate->scene('mobile')->check($post);
                if ($rst) {
                    if (captcha_check($post['verify'])){
                        $count = db('ns_user')->where('mobile',$post['mobile'])->count();
                        if ($count < 1) {
                            $str = '';
                            for($i = 0;$i<4;$i++){
                                $str .= rand(0,9);
                            }
                            cookie('code',$str,60);
                            cookie('mobile',$post['mobile'],120);
                            $send = SmsDemo::sendSms($post['mobile'],['code'=>$str]);
                            $this->success('短信已发送');
                        }
                        $this->error('该账号已注册');
                    }
                    $this->error('验证码不正确');
                }
                $this->error('手机格式不正确');
            }
            $this->error('不可以为空哦');
        }
        $this->error('您属于非法请求哦');
    }

    public function focode()
    {
        if (is_post()) {
            $post = input('post.');
            if (self::are_null($post)) {
                $validate = validate('Vnsuser');
                $rst = $validate->scene('mobile')->check($post);
                if ($rst) {
                    $count = db('ns_user')->where('mobile',$post['mobile'])->count();
                    if ($count) {
                        $str = '';
                        for($i = 0;$i<4;$i++){
                            $str .= rand(0,9);
                        }
                        cookie('code',$str,60);
                        cookie('mobile',$post['mobile'],120);
                        $send = SmsDemo::sendSms($post['mobile'],['code'=>$str]);
                        $this->success('短信已发送');
                    }
                    $this->error('该账号不存在');
                }
                $this->error('手机格式不正确');
            }
            $this->error('不可以为空哦');
        }
        $this->error('您属于非法请求哦');
    }

    public function forget()
    {
        if(is_post()){
            $post = input('post.');
            $info = [];
            if($post['code'] == cookie('code')){
                $info['password'] = encrypt($post['password']);
                $row = db('ns_user')->where('mobile',$post['mobile'])->update($info);
                if ($row) {
                    $this->success('密码重置成功，请登录');
                }
                $this->error('请稍后再试');
            }
            $this->error('验证码不正确');
        }
        $this->error('您属于非法请求哦');
    }

    public function rpwd ()
    {
        $post = input('post.');
        // return $post;
        $id = Session::get('aid');
        if ($id) {
            if ($post['newpwd']==$post['rpwd']) {
                $password = db('ns_user')->where('id',$id)->value('password');
                if (encrypt($post['oldpwd']) == $password ) {
                    $rpwd = encrypt($post['rpwd']);
                    if (db('ns_user')->where('id',$id)->update(['password'=>$rpwd])) {
                        $this->success('修改成功');
                    }
                    $this->error('请稍后再试');
                }
                $this->error('原始密码错误');
            }
            $this->error('两次密码不一致');
        }
        $this->error('请登录');
    }

    public function bspwd ()
    {
        $post = input('post.');
        // return $post;
        $id = Session::get('aid');
        if ($post['bspwd'] == $post['rbspwd']) {
            $busspwd = db('ns_user')->where('id',$id)->value('busspwd');
            if($busspwd){
                if(encrypt($post['oldbspwd']) != $busspwd){
                    $this->error('原始交易密码错误');
                } 
            }
            $busspwd = encrypt($post['bspwd']);
                if(db('ns_user')->where('id',$id)->update(['busspwd'=>$busspwd])){
                    $this->success('修改成功');
                }
                $this->error('请稍后再试');
        }
        $this->error('两次密码不一致');
    }

    public function safe(){
        $id = Session::get('aid');
        $post = input('post.');
        if(db('ns_user')->where('id',$id)->update(['s_pwd'=>$post['str1'].','.$post['str2'].','.$post['str2']])){
            $this->success('修改成功');
        }
        $this->error('请稍后再试');
    }

    public function perfect(){
        $id = Session::get('aid');
        $post = input('post.');
        if(db('ns_user')->where('id',$id)->update($post)){
            $this->success('修改成功');
        }
        $this->error('请稍后重试');
    }

    public function redenve(){
        if (!is_post())
            $this->error('您属于非法请求哦');
        $id = Session::get('aid');
        if(empty($id))
            $this->error('请先登录');
        $user = db('ns_user')->where('id',$id)->find();
        if($user['perfect']==0)
            $this->error('完善信息后可以领取哦');
        $tips = db('tips_base')->select();
        if(empty($tips)){
            $this->error('目前无红包可领');
        }
        $info = [];
        $where = [];
        $where['uid'] = $id;
        foreach($tips as $k=>$v) {
            $where['tip'] = $v['id'];
            $log = db('tips_log')->where($where)->find();
            if(empty($log)){
                // 红包金额
                $rand = rand($v['min'],$v['max']);
                $tdata = [];
                $tdata['username'] = $user['username'];
                $tdata['mobile'] = $user['mobile'];
                $tdata['uid'] = $id;
                $tdata['tip'] = $v['id'];
                $tdata['tname'] = $v['name'];
                $tdata['rec_time'] = date('Y-m-d H:i:s');
                $tdata['rec_paper'] = $rand;
                db('tips_log')->insert($tdata);
                $user['total_funds'] += $rand;
                $user['available_funds'] += $rand;
                db('ns_user')->where('id',$id)->update($user);
                $this->success('领取'.$v['name'].'成功');
            }
        }
        $this->error('现有的红包都领过了哦');
    }
    // public function index()
    // {

    // 	$this->error('no access');
    // }

    // // 公告列表
    // public function noticeList()
    // {
    //     // 获取最新公告
    // 	$list = db('notice')->where('status', 1)->order('create_time DESC')->find();

    //     $list['n_message'] = html_entity_decode($list['n_message']);

    // 	return $this->result($list);
    // }

    //  // 公告列表
    // public function noticeLists()
    // {
    //     // 获取最新公告
    //     $list = db('notice')->where('status', 1)->order('create_time DESC')->select();
    //     foreach ($list as $k => $v) {
    //         $list[$k]['n_message'] = html_entity_decode($v['n_message']);
    //     }

    //     // $list['n_message'] = html_entity_decode($list['n_message']);

    //     return $this->result($list);
    // }

    // // 公告info 
    // public function getNotice()
    // {
    // 	$id = input('param.id');
    // 	$info = db('notice')->where('id', $id)->find();
    //     $info['n_message'] = html_entity_decode($info['n_message']);
    // 	return $this->result($info);
    // }

    // public function miss()
    // {
    // 	$this->error('no access');
    // }



}